home *** CD-ROM | disk | FTP | other *** search
- #include <string.h>
-
- #include "ScriptScrap.h"
- #include "Prototypes.h"
-
-
- /* Constants for the message strings which appear in our error dialog */
- const short kUrgent = 3001;
- const short kIn = 3002;
-
- /* Constants for our message dialogs */
- const short kFatalErr = 2003;
- const short kWarning = 2004;
- const short kNotice = 2005;
-
- typedef struct {
- short indices[100];
- char text[2]; /* Pascal-style error strings begin here */
- } ErrorStrings, *ErrorStringsPtr, **ErrorStringsHand;
-
- /* Prototypes for private routines */
-
- static void _ErrorDialog(char *errorMsg, char *location, Boolean isFatal);
- static short _CheckPointer(void *p);
-
- static void _ErrorDialog (char *errorMsg, char *location, Boolean isFatal)
- {
- enum {
- kQuit = 1,
- kDebugger,
- kContinue
- };
-
- char inParam[4];
- short result;
- StringHandle str_hndl;
-
- CtoPstr(errorMsg); CtoPstr(location);
- if (location[0] == 0)
- ParamText((StringPtr)errorMsg, (void*)"", (StringPtr)location, (void*)"");
- else {
- str_hndl = GetString ( kIn );
- if ( str_hndl )
- {
- HLock ( (void *)str_hndl );
- ParamText((StringPtr)errorMsg, (StringPtr)*str_hndl, (StringPtr)location, (void*)"");
- HUnlock ( (void *)str_hndl );
- ReleaseResource ( (void *)str_hndl );
- }
- }
- PtoCstr((unsigned char *)errorMsg); PtoCstr((unsigned char *)location);
- MyInteractWithUser(true); /* Call the notification manager & make sure we're in front */
- if (isFatal)
- result = StopAlert(kFatalErr, NULL);
- else
- result = CautionAlert(kWarning, NULL);
- if (result == kDebugger)
- Debugger();
- if (isFatal || (result == kQuit))
- ExitToShell();
- } /* _ErrorDialog */
-
-
- static short _CheckPointer (void *p)
- {
- if (p == NULL)
- return kPointerNULL;
-
- if ((long)p & 0x00000001)
- return kPointerOdd;
-
- return kPointerOK;
- } /* _CheckPointer */
-
-
- void Assert (Boolean condition, char *message, char *location, Boolean isFatal)
- /* Notify the user if the specified condition isn't true. If isFatal is TRUE, */
- /* exit to the Finder afterwards */
- {
- char messageCopy[256];
-
- if (!condition) {
- strcpy(messageCopy, "Assertion failed: ");
- strcat(messageCopy, message);
- _ErrorDialog(messageCopy, location, isFatal);
- }
- } /* Assert */
-
-
- Boolean AssertOK (Boolean condition, char *message, char *location)
- /* Notify the user if the specified condition isn't true, and return "condition" */
- /* for use in an if statement. */
- {
- Assert(condition, message, location, FALSE);
- return condition;
- } /* AssertOK */
-
-
- void CheckPointer (void *p, char *ptrName, char *location)
- {
- char errorMsg[256];
-
- strcpy(errorMsg, ptrName);
- switch (_CheckPointer(p)) {
- case kPointerOK:
- return;
-
- case kPointerNULL:
- strncat(errorMsg, " is NULL", 255);
- break;
-
- case kPointerOdd:
- strncat(errorMsg, " is odd", 255);
- break;
-
- };
- _ErrorDialog(errorMsg, location, kIsFatal);
- } /* CheckPointer */
-
-
- void CheckHandle (void *h, char *handleName, char *location)
- {
- char place_holder[257];
-
- strcpy( place_holder, "*" );
- strcat( place_holder, handleName );
- CheckPointer(h, place_holder, location);
- strcpy( place_holder, "**" );
- strcat( place_holder, handleName );
- CheckPointer(*((Handle)h), place_holder, location);
- } /* CheckHandle */
-
-
-
- void CheckResult (short code, char *location)
- {
- Str255 errorMsg, errorNum;
- ErrorStringsHand messageRsrc;
- long messageOffset;
- Str255 message;
- short rsrcNum;
-
- if (code) {
- NumToString(code, errorNum);
- PtoCstr((unsigned char *)errorNum);
- strcpy((char *)errorMsg, "Error code ");
- strcat((char *)errorMsg, (char *)errorNum);
-
- /* Now, append the error's name to our message */
- strcpy((char *)message, "Unknown");
- rsrcNum = code / 100;
- /* if the error number is negative, then the resource ID should be negative */
- /* If the error number is positive, we make sure the resource ID is > 0 (to */
- /* avoid confusion as to whether 0 should be used for positive or negative */
- /* error codes. */
- if (rsrcNum > 0)
- ++rsrcNum;
- else
- --rsrcNum;
-
- messageRsrc = (ErrorStringsHand)GetResource('ERRS', rsrcNum); /* Each error string resource holds a range of 100 error numbers */
- if (messageRsrc != NULL) {
- if (code < 0)
- code = - code;
- messageOffset =(*messageRsrc)->indices[code % 100] - (100 * sizeof(short));
- if (messageOffset != 0) {
- BlockMove(&((*messageRsrc)->text[messageOffset]), message, (*messageRsrc)->text[messageOffset] + 1);
- PtoCstr(message);
- }
- }
-
- strcat((char *)errorMsg, " (");
- strcat((char *)errorMsg, (char *)message);
- strcat((char *)errorMsg, ")");
- _ErrorDialog((char*)errorMsg, location, kIsFatal);
- };
- } /* CheckResult */
-
-
- void Error (char *errorMsg, char *location)
- {
- _ErrorDialog(errorMsg, location, kIsFatal);
- } /* Error */
-
-
- Boolean IsValidPointer (void *p)
- {
- return _CheckPointer(p) == kPointerOK;
- } /* IsValidPointer */
-
-
- Boolean IsValidHandle (void *h)
- {
- return IsValidPointer(h) && IsValidPointer(*(void **)h);
- } /* IsValidHandle */
-
-
- void ShowMessageAndDie (char *message)
- /* This routine displays an error message and then aborts back to the main event loop */
- {
- _ErrorDialog(message, "", kNonFatal);
- } /* ShowMessageAndDie */
-
-
- void ShowMessage (char *message)
- /* This routine displays an error message and then continues execution */
- {
- short itemHit;
-
- CtoPstr(message);
- ParamText((StringPtr)message, (void*)"", (void*)"", (void*)"");
- PtoCstr((unsigned char *)message);
- MyInteractWithUser(true); /* Call the notification manager & make sure we're in front */
- itemHit = NoteAlert(kNotice, NULL);
- if (itemHit == 1)
- ExitToShell();
- } /* ShowMessage */
-